package com.fluxtool.utill.aspect;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.fluxtool.utill.annotation.FluxLog;

@Component 
@Aspect
@Configuration
public class FluxAspect {
	
	private static final Logger log = LoggerFactory.getLogger(FluxAspect.class);
				
	@Pointcut("@annotation(com.fluxtool.utill.annotation.FluxLog)")
	public void serviceAspect(){}
	
	@Around("serviceAspect()")
	public void doServiceLog(JoinPoint joinPoint){
		log.info("日志记录");
	    try {
	    	getServiceMthodDescription(joinPoint);  
	    }catch (Exception e) {    
	    	log.error("异常信息:{}", e);
	    }
	}
	
    /** 
     * getServiceMthodDescription:获取注解中对方法的描述信息 用于service层注解  . <br/> 
     * @author lcma 
     * @param joinPoint 切点  
     * @return 方法描述 
     * @throws Exception  
     * @since JDK 1.7 
     */  
    @SuppressWarnings("rawtypes")
	private String getServiceMthodDescription(JoinPoint joinPoint) throws Exception {  
        String targetName = joinPoint.getTarget().getClass().getName(); 
  		log.info("targetName:"+targetName); 
        String methodName = joinPoint.getSignature().getName();
  		log.info("methodName:"+methodName);  
        Object[] arguments = joinPoint.getArgs();  
  		log.info("arguments:"+arguments);
        Class targetClass = Class.forName(targetName); 
  		log.info("targetClass:"+targetClass); 
        Method[] methods = targetClass.getMethods();  
  		log.info("methods:"+methods); 
        String description = "";  
         for(Method method : methods) {  
             if(method.getName().equals(methodName)) {  
                Class[] clazzs = method.getParameterTypes();  
                if(clazzs.length == arguments.length) {  
                    description = method.getAnnotation(FluxLog.class).value();
              		log.info("description:"+description); 
                     break;  
                }  
            }  
        }  
        return description;  
      }  
}
